package com.repair.miniApp.controller;

import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.repair.common.core.domain.AjaxResult;
import com.repair.miniApp.domain.MiniAppUsers;
import com.repair.miniApp.service.IMiniAppUsersService;

/**
 * 小程序用户Controller
 * 区分普通用户和工作人员
 * 
 * @author repair
 * @date 2024-12-05
 */
@RestController
@RequestMapping("/miniApp/users")
public class MiniAppUserController
{
    @Autowired
    private IMiniAppUsersService miniAppUserService;

    /**
     * 用户登录
     * 
     * @param miniAppUser 小程序用户
     * @return 登录结果
     */
    // @PreAuthorize("permitAll")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody MiniAppUsers miniAppUser)
    {
        MiniAppUsers user = miniAppUserService.login(miniAppUser);
        if (user != null) {
            return AjaxResult.success(user);
        } else {
            return AjaxResult.error("登录失败，账号或密码错误");
        }
    }

    /**
     * 用户注册
     * 
     * @param miniAppUser 小程序用户
     * @return 注册结果
     */
    // @PreAuthorize("permitAll")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody MiniAppUsers miniAppUser)
    {
        int result = miniAppUserService.register(miniAppUser);
        if (result == 1) {
            return AjaxResult.success("注册成功");
        } else if (result == -1) {
            return AjaxResult.error("用户已存在");
        } else {
            return AjaxResult.error("注册失败");
        }
    }
}
